package com.demo2do.boyle.client.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.client.cache.accessor.PersistentAccessor;
import com.demo2do.boyle.client.service.CustomerProfileService;
import com.demo2do.boyle.common.dao.TagDaoSupport;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.Profile;
import com.demo2do.boyle.common.entity.customer.Sex;
import com.demo2do.boyle.common.entity.geo.Geo;
import com.demo2do.boyle.common.entity.system.tag.Tag;
import com.demo2do.boyle.common.entity.system.tag.Type;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;

@Service("customerProfileService")
public class CustomerProfileServiceImpl extends GenericServiceImpl<Customer> implements CustomerProfileService {
	
	@Autowired
    private GenericDaoSupport genericDaoSupport;
	
	@Autowired
	private TagDaoSupport tagDaoSupport;
    
    @Autowired
	private PersistentAccessor persistentAccessor;
    
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerProfileService#editSex(java.lang.String)
	 */
	public void editSex(Sex sex, Customer customer) {
		
		Map<String, Object> sexes =  persistentAccessor.getSexes();
		
		Tag sexTag = null;
		
		for(String key : sexes.keySet()) {
			Tag tagNow = (Tag) sexes.get(key);
			if(tagNow.getName().equalsIgnoreCase(sex.getDescription())) {
				sexTag = tagNow;
			}
		}
		
		if(customer.getProfile() == null) {
			Profile profile = new Profile();
			profile.setSex(sex);
			customer.setProfile(profile);
		}else {
			customer.getProfile().setSex(sex);
		}
		
		customer.getCustomerTag().setSexTag(sexTag.getCode());

		genericDaoSupport.update(customer);
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerProfileService#editAge(java.lang.Integer, com.demo2do.boyle.common.entity.customer.Customer)
	 */
	public void editAge(Integer age, Customer customer) {
		
		List<Tag> ageTags = tagDaoSupport.list(Type.AGE);
		Tag ageTag = null; 
		
		for(int i = 0; i<ageTags.size(); i++) {
			
			// FIXME here
			if(ageTags.get(i).value >= age && i==0) {
				ageTag = ageTags.get(i);
			}else if( ageTags.get(i).value < age && ageTags.get(i+1).value >= age){
				ageTag = ageTags.get(i+1);
			}
		}
		
		if(customer.getProfile() == null) {
			Profile profile = new Profile();
			profile.setAge(age);
			customer.setProfile(profile);
		}else {
			customer.getProfile().setAge(age);
		}
		customer.getCustomerTag().setAgeTag(ageTag.getCode());
		
		genericDaoSupport.update(customer);
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerProfileService#editNickName(java.lang.String, com.demo2do.boyle.common.entity.customer.Customer)
	 */
	public void editNickName(String nickname, Customer customer) {
		
		if(customer.getProfile() == null) {
			Profile profile = new Profile();
			profile.setNickName(nickname);
			customer.setProfile(profile);
		}else {
			customer.getProfile().setNickName(nickname);
		}
		genericDaoSupport.update(customer);
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerProfileService#editArea(java.lang.Integer, com.demo2do.boyle.common.entity.customer.Customer)
	 */
	public void editArea(Geo city, Geo province, Customer customer) {
		
		Map<String, Object> areas =  persistentAccessor.getAreas();
		Tag areaTag = null;
		
		for(String key : areas.keySet()) {
			Tag tagNow = (Tag) areas.get(key);
			if( city.getName().equalsIgnoreCase( tagNow.getName() ) ) {
				areaTag = tagNow;
			}
		}
		
		if(customer.getProfile() == null) {
			Profile profile = new Profile();
			profile.setProvince(province);
			profile.setCity(city);
			customer.setProfile(profile);
		}else {
			customer.getProfile().setCity(city);
			customer.getProfile().setProvince(province);
		}
		if(areaTag != null){
			customer.getCustomerTag().setAreaTag(areaTag.getCode());
		}else {
			customer.getCustomerTag().setAreaTag(0);
		}
		
		genericDaoSupport.update(customer);
		
	}

}
